home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1993 July / InfoMagic USENET CD-ROM July 1993.ISO / sources / misc / volume14 / ds / part01 next >
Encoding:
Text File  |  1990-08-29  |  4.3 KB  |  162 lines

  1. Newsgroups: comp.sources.misc
  2. organization: Stanford Relativity Gyroscope Experiment (GP-B)
  3. subject: v14i064: 'ds': Disk Space Available
  4. From: mike@relgyro.stanford.edu (Mike Macgirvin)
  5. Sender: allbery@uunet.UU.NET (Brandon S. Allbery - comp.sources.misc)
  6.  
  7. Posting-number: Volume 14, Issue 64
  8. Submitted-by: mike@relgyro.stanford.edu (Mike Macgirvin)
  9. Archive-name: ds/part01
  10.  
  11.     A program to report the available disk space, and ONLY the available
  12. disk space. This is possibly BSD specific. I wrote this after several
  13. generations of programs which used 'awk' to grab the one field of interest
  14. from 'df'. With no arguments, it reports the number of bytes available
  15. in the filesystem containing the current directory. Options can report the
  16. free space in other units, and with a label. Note that 1k == 1000 bytes, not
  17. 1024. You may wish to change this.
  18.  
  19.  
  20.  
  21. #! /bin/sh
  22. # This is a shell archive, meaning:
  23. # 1. Remove everything above the #! /bin/sh line.
  24. # 2. Save the resulting text in a file.
  25. # 3. Execute the file with /bin/sh (not csh) to create the files:
  26. #    ds.c
  27. # This archive created: Mon Aug 27 14:55:39 1990
  28. export PATH; PATH=/bin:$PATH
  29. echo shar: extracting "'ds.c'" '(2514 characters)'
  30. if test -f 'ds.c'
  31. then
  32.     echo shar: will not over-write existing file "'ds.c'"
  33. else
  34. sed 's/^    X//' << \SHAR_EOF > 'ds.c'
  35.     X/* ds.c */
  36.     X
  37.     X
  38.     X/* 
  39.     X *   ds - disk space
  40.     X *   usage:
  41.     X *          ds [-gmkv] [path]
  42.     X *                 -g   - Report size in gigabytes
  43.     X *                 -m   - Report size in megabytes
  44.     X *                 -k   - Report size in kilobytes
  45.     X *                 -v   - Show label (bytes,kilobytes,or megabytes)
  46.     X *                 path - Directory or file designating the partition
  47.     X *                        to be reported. Default is current directory.
  48.     X *
  49.     X *          The report is displayed in integer units, unless the result is
  50.     X *  less than 1.0, in which case the report is extended to one decimal point.
  51.     X *  Divisors are decimal, not binary; i.e. 1 'kilobyte' is equal to 1000 bytes,
  52.     X *  not 1024 bytes.
  53.     X */
  54.     X
  55.     X/*
  56.     X *
  57.     X * Copyright (C) 1990 by Mike Macgirvin. All Rights Reserved.
  58.     X *
  59.     X * Permission to use, copy, modify, and distribute this software and its
  60.     X * documentation for any purpose and without fee is hereby granted, provided
  61.     X * that the above copyright notice appear in all copies and that both that
  62.     X * copyright notice and this permission notice appear in supporting
  63.     X * documentation.  This software is provided "as is" without express or
  64.     X * implied warranty.
  65.     X *
  66.     X */
  67.     X
  68.     X
  69.     X
  70.     X#include <stdio.h>
  71.     X#include <strings.h>
  72.     X#include <sys/types.h>
  73.     X#include <sys/param.h>
  74.     X#include <sys/vfs.h>
  75.     X
  76.     X#define K_BYTES 1.0E+3
  77.     X#define M_BYTES 1.0E+6
  78.     X#define G_BYTES 1.0E+9
  79.     X
  80.     X#define STRCPY (void)strcpy
  81.     X
  82.     Xextern int optind;
  83.     Xextern char *optarg;
  84.     X
  85.     Xstruct statfs stats;
  86.     X
  87.     Xchar path[MAXPATHLEN];
  88.     Xchar label[32];
  89.     Xchar fmt[32];
  90.     Xmain(argc,argv)
  91.     X     int argc;
  92.     X     char **argv;
  93.     X{
  94.     X  int c;
  95.     X  int sflag = 0;
  96.     X  int vflag = 0;
  97.     X  double space;
  98.     X  while((c = getopt(argc,argv,"gmkv")) != EOF) {
  99.     X    switch(c) {
  100.     X    case 'g':
  101.     X      sflag = 3;
  102.     X      break;
  103.     X    case 'm':
  104.     X      sflag = 2;
  105.     X      break;
  106.     X    case 'k':
  107.     X      sflag = 1;
  108.     X      break;
  109.     X    case 'v':
  110.     X      vflag ++;
  111.     X      break;
  112.     X    }
  113.     X  }
  114.     X  if(optind < argc)
  115.     X    STRCPY(path,argv[optind]);
  116.     X  else
  117.     X    STRCPY(path,".");
  118.     X     
  119.     X  if(statfs(path,&stats)) {
  120.     X    perror("statfs");
  121.     X    exit(1);
  122.     X  }
  123.     X  space = (double) (stats.f_bavail * stats.f_bsize);
  124.     X  switch(sflag) {
  125.     X  case 1:
  126.     X    space = space / K_BYTES;
  127.     X    STRCPY(label," Kilobytes");
  128.     X    break;
  129.     X  case 2:
  130.     X    space = space / M_BYTES;
  131.     X    STRCPY(label," Megabytes");
  132.     X    break;
  133.     X  case 3:
  134.     X    space = space / G_BYTES;
  135.     X    STRCPY(label," Gigabytes");
  136.     X    break;
  137.     X  default:
  138.     X    STRCPY(label," Bytes");
  139.     X    break;
  140.     X  }
  141.     X  if(space < 1.0)
  142.     X    STRCPY(fmt,"%-.1f%s\n");
  143.     X  else
  144.     X    STRCPY(fmt,"%-.0f%s\n");
  145.     X
  146.     X  (void) printf(fmt,space,(vflag) ? label : "");
  147.     X
  148.     X}
  149. SHAR_EOF
  150. if test 2514 -ne "`wc -c < 'ds.c'`"
  151. then
  152.     echo shar: error transmitting "'ds.c'" '(should have been 2514 characters)'
  153. fi
  154. fi # end of overwriting check
  155. #    End of shell archive
  156. exit 0
  157.     ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  158.     +  Mike Macgirvin       mike@Jimi-Hendrix.Stanford.EDU +
  159.     ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  160.  
  161.  
  162.